<template>
	<view class="content">
		<view>{{userInfo.nickname}}</view>
		<view>当前鸭鸭卡：{{userInfo.source}}</view>
		<view class="box" :class="isUpdate ? 'update' : ''">
			<view class="title">
				<view>个人信息</view>
				<text v-if="!isUpdate" @click="isUpdate = true">编辑</text>
			</view>
			<view class="item">
				<view>
					<uni-icons type="person" size="20"></uni-icons>
					姓名
				</view>
				<view v-if="!isUpdate">{{editUserInfo.nickname}}</view>
				<input v-else type="text" v-model="editUserInfo.nickname">
			</view>
			<view class="item">
				<view>
					<uni-icons type="phone" size="20"></uni-icons>
					联系方式
				</view>
				<view v-if="!isUpdate">{{editUserInfo.mobile}}</view>
				<input v-else type="text" v-model="editUserInfo.mobile">
			</view>
			<view class="item">
				<view>
					<uni-icons type="location" size="20"></uni-icons>
					收件地址
				</view>
				<view v-if="!isUpdate">{{editUserInfo.addr}}</view>
				<input v-else type="text" v-model="editUserInfo.addr">
			</view>
		</view>
		<view v-if="isUpdate" class="submit" @click="submit">保存修改</view>
	</view>
</template>

<script>
	import {
		getAction,
		postAction
	} from '../../../common/request'
	export default {
		data() {
			return {
				isUpdate: false,
				userInfo: {},
				editUserInfo: {}
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				getAction('/user').then(res => {
					this.userInfo = {
						...res.data
					}
					this.editUserInfo = {
						...res.data
					}

				})
			},
			submit() {
				
				if (!this.editUserInfo.nickname) return uni.showToast({
					title: '请输入姓名',
					icon: 'none'
				})
				
				if (!this.editUserInfo.mobile) return uni.showToast({
					title: '请输入电话',
					icon: 'none'
				})
				
				if (!this.editUserInfo.addr) return uni.showToast({
					title: '请输入地址',
					icon: 'none'
				})
				wx.showLoading()
				postAction('/user', {
					...this.editUserInfo
				}).then(res => {
					wx.hideLoading()
					if (res.code === 0) {
						uni.showToast({
							title: '修改成功'
						})
						this.getUserInfo()
						this.isUpdate = false
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding-top: 120rpx;
		color: #2B2B2B;
		box-sizing: border-box;

		>view:nth-of-type(1) {
			text-align: center;
			font-size: 60rpx;
			min-height: 72rpx;
			line-height: 72rpx;
			margin-bottom: 48rpx;
		}

		>view:nth-of-type(2) {
			text-align: center;
			font-size: 28rpx;
			line-height: 36rpx;
			min-height: 36rpx;
			margin-bottom: 158rpx;
		}

		.box {
			color: #2B2B2B;
			width: 690rpx;
			background: #F5F5F5;
			border-radius: 10rpx 10rpx 10rpx 10rpx;
			background: rgba(245, 245, 245, 1);
			margin: 0 auto;
			padding: 34rpx 30rpx;
			font-size: 28rpx;
			height: 480rpx;
		box-sizing: border-box;

			.title {
				display: flex;
				align-items: center;
				justify-content: space-between;

				line-height: 36rpx;
				margin-bottom: 37rpx;

				text {
					color: #FF5035;
				}
			}

			.item {
				padding-left: 50rpx;
				margin-bottom: 20rpx;

				>view:nth-of-type(1) {
					.uni-icons {
						vertical-align: middle;
						margin-top: -6rpx;
						margin-left: -50rpx;
						margin-right: 10rpx;
					}
				}

				input {
					text-align: right;
				}
			}
		}

		.box.update {
			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 37rpx;
				border-bottom: 1px solid #6a6a6a;
				padding-bottom: 18rpx;
			}
		}

		.submit {

			width: 650rpx;
			height: 100rpx;
			background: #FF5035;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			margin: 100rpx auto 0 auto;
			font-size: 32rpx;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 36rpx;
			text-align: center;
			line-height: 100rpx;
		}
	}
</style>